﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WitsWebApi.DataContext.DapperRepositories;
using WitsWebApi.Entity.Entities;
using WitsWebApi.Entity.EntityParam;
using WitsWebApi.Jwt;

namespace WitsWebApi.Controllers
{
    /// <summary>
    /// 机器设备详情控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Policy = "SystemOrAdmin")]
    public class MachineController : ControllerBase
    {
        private readonly IMachineRepository _IMachineRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="m__IMachineRepository"></param>
        public MachineController(IMapper m_mapper, IMachineRepository m_IMachineRepository)
        {
            _mapper = m_mapper;
            _IMachineRepository = m_IMachineRepository;
        }

        /// <summary>
        /// 获取指定设备信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetMachineDetailByName")]
        public async Task<ActionResult<Machine>> GetMachineDetailByName(string MachineName)
        {
            try
            {
                var result = await _IMachineRepository.GetMachineDetailByName(MachineName);

                return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 添加设备
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddMachine")]
        public async Task<ActionResult<int>> AddMachine(MachineParam machineParam)
        {
            try
            {
                var result = await _IMachineRepository.AddMachine(machineParam);

                return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetPageList")]
        public async Task<ActionResult> GetPageList(MachinePageResult machinePageResult)
        {
            try
            {
                var result = await _IMachineRepository.GetPageList(machinePageResult);

                return Ok(result);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
